package com.example.dzbsh.controller;

import com.example.dzbsh.model.Dept;
import com.example.dzbsh.service.DaochuService;
import com.example.dzbsh.service.DeptService;
import com.example.dzbsh.service.RenyuanService;
import com.example.dzbsh.util.CreateWord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("daochu")
public class DaochuController {

	@Resource
	DaochuService ddDaochuService;
	@Resource
	DeptService ddDeptService;
	@Resource
	RenyuanService ddRenyuanService;
	@Value("${userfilepath}")
	private String userfilepath;


	// 导出经商情况json
	@RequestMapping("jingshangqingkuang")
	public void Jingshangqingkuang(HttpServletRequest request,
											HttpServletResponse response) {
		try {
			ddDaochuService.DaochuJingshangQingkuang(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 委托查核经商办企业信息汇总表
	@RequestMapping("qiyexinxihuizong")
	public void Qiyexinxihuizong(@RequestParam("deptid") String ppDeptId, HttpServletRequest request,
										  HttpServletResponse response, ServletResponse servletResponse) {
		try {
			ddDaochuService.createPaichuHuoxuandengji(ppDeptId, request, response, servletResponse);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 单位查核数据导出
	@RequestMapping("chaheshuju")
	public void chaheshuju(@RequestParam("deptid") String ppDeptId,@RequestParam("isall") boolean ppIsAll,
						   HttpServletRequest request,HttpServletResponse response, ServletResponse servletResponse) {
		try {
			ddDaochuService.DaochuChaheQingkuang(ppDeptId,ppIsAll,request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 单位甄别汇总表导出
	@RequestMapping("zhenbiehuizong")
	public void ZhenbieHuizong(@RequestParam("deptid") String ppDeptId,@RequestParam("renyuanleibie") String ppRenyuanLeibieId,
							   @RequestParam("jigoumingchengma") String ppJigoumingchengma,@RequestParam("paixu") String ppPaixu,
							   HttpServletRequest request,HttpServletResponse response, ServletResponse servletResponse) {
		try {
			ddDaochuService.Zhenbiehuizong(ppDeptId,ppRenyuanLeibieId,ppJigoumingchengma,ppPaixu,request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("daochu_guifantongji")
	public void daochuGuifanTongji(@RequestParam("deptid") String ppDeptId, HttpServletRequest request, HttpServletResponse response){

		try {
			Map<String,Object> mmResultMap = new HashMap<>();

			Dept mmDept = ddDeptService.findOne(ppDeptId);

			String mmDeptName = "";
			if (mmDept!=null){
				mmDeptName = mmDept.getDanweiduiwaimingcheng() == null ? "" : mmDept.getDanweiduiwaimingcheng();
			}

			Map<String,Object> mmGuifanMap = ddRenyuanService.findGuifanTongjibiao(ppDeptId);

			String mmQuanbu = mmGuifanMap.get("quanbu")==null?"":mmGuifanMap.get("quanbu").toString();
			String mmShangjiangquanbu = mmGuifanMap.get("shangjiangquanbu")==null?"":mmGuifanMap.get("shangjiangquanbu").toString();
			String mmZhongjiangquanbu = mmGuifanMap.get("zhongjiangquanbu")==null?"":mmGuifanMap.get("zhongjiangquanbu").toString();
			String mmShaojiangzhengquanbu = mmGuifanMap.get("shaojiangzhengquanbu")==null?"":mmGuifanMap.get("shaojiangzhengquanbu").toString();
			String mmShaojiangfuquanbu = mmGuifanMap.get("shaojiangfuquanbu")==null?"":mmGuifanMap.get("shaojiangfuquanbu").toString();
			String mmDaxiaozhengquanbu = mmGuifanMap.get("daxiaozhengquanbu")==null?"":mmGuifanMap.get("daxiaozhengquanbu").toString();
			String mmDaxiaofuquanbu = mmGuifanMap.get("daxiaofuquanbu")==null?"":mmGuifanMap.get("daxiaofuquanbu").toString();


			String mmJingshang = mmGuifanMap.get("jingshang")==null?"":mmGuifanMap.get("jingshang").toString();
			String mmShangjiangjingshang = mmGuifanMap.get("shangjiangjingshang")==null?"":mmGuifanMap.get("shangjiangjingshang").toString();
			String mmZhongjiangjingshang = mmGuifanMap.get("zhongjiangjingshang")==null?"":mmGuifanMap.get("zhongjiangjingshang").toString();
			String mmShaojiangzhengjingshang = mmGuifanMap.get("shaojiangzhengjingshang")==null?"":mmGuifanMap.get("shaojiangzhengjingshang").toString();
			String mmShaojiangfujingshang = mmGuifanMap.get("shaojiangfujingshang")==null?"":mmGuifanMap.get("shaojiangfujingshang").toString();
			String mmDaxiaozhengjingshang = mmGuifanMap.get("daxiaozhengjingshang")==null?"":mmGuifanMap.get("daxiaozhengjingshang").toString();
			String mmDaxiaofujingshang = mmGuifanMap.get("daxiaofujingshang")==null?"":mmGuifanMap.get("daxiaofujingshang").toString();
			String mmShangxiaozhengJingshang = mmGuifanMap.get("shangxiaozhengjingshang")==null?"":mmGuifanMap.get("shangxiaozhengjingshang").toString();
			String mmShangxiaozhengQuanbu = mmGuifanMap.get("shangxiaozhengquanbu")==null?"":mmGuifanMap.get("shangxiaozhengquanbu").toString();

			mmResultMap.put("quanbu",mmQuanbu);
			mmResultMap.put("shangjiangquanbu",mmShangjiangquanbu);
			mmResultMap.put("zhongjiangquanbu",mmZhongjiangquanbu);
			mmResultMap.put("shaojiangzhengquanbu",mmShaojiangzhengquanbu);
			mmResultMap.put("shaojiangfuquanbu",mmShaojiangfuquanbu);
			mmResultMap.put("daxiaozhengquanbu",mmDaxiaozhengquanbu);
			mmResultMap.put("daxiaofuquanbu",mmDaxiaofuquanbu);
			mmResultMap.put("jingshang",mmJingshang);
			mmResultMap.put("shangjiangjingshang",mmShangjiangjingshang);
			mmResultMap.put("zhongjiangjingshang",mmZhongjiangjingshang);
			mmResultMap.put("shaojiangzhengjingshang",mmShaojiangzhengjingshang);
			mmResultMap.put("shaojiangfujingshang",mmShaojiangfujingshang);
			mmResultMap.put("daxiaozhengjingshang",mmDaxiaozhengjingshang);
			mmResultMap.put("daxiaofujingshang",mmDaxiaofujingshang);
			mmResultMap.put("shangxiaozhengjingshang",mmShangxiaozhengJingshang);
			mmResultMap.put("shangxiaozhengquanbu",mmShangxiaozhengQuanbu);

			String mmTcQuanbu = mmGuifanMap.get("tcquanbu")==null?"":mmGuifanMap.get("tcquanbu").toString();
			String mmTcShangjiangquanbu = mmGuifanMap.get("tcshangjiangquanbu")==null?"":mmGuifanMap.get("tcshangjiangquanbu").toString();
			String mmTcZhongjiangquanbu = mmGuifanMap.get("tczhongjiangquanbu")==null?"":mmGuifanMap.get("tczhongjiangquanbu").toString();
			String mmTcShaojiangzhengquanbu = mmGuifanMap.get("tcshaojiangzhengquanbu")==null?"":mmGuifanMap.get("tcshaojiangzhengquanbu").toString();
			String mmTcShaojiangfuquanbu = mmGuifanMap.get("tcshaojiangfuquanbu")==null?"":mmGuifanMap.get("tcshaojiangfuquanbu").toString();
			String mmTcDaxiaozhengquanbu = mmGuifanMap.get("tcdaxiaozhengquanbu")==null?"":mmGuifanMap.get("tcdaxiaozhengquanbu").toString();
			String mmTcDaxiaofuquanbu = mmGuifanMap.get("tcdaxiaofuquanbu")==null?"":mmGuifanMap.get("tcdaxiaofuquanbu").toString();
			String mmTcShangxiaozhengQuanbu = mmGuifanMap.get("tcshangxiaozhengquanbu")==null?"":mmGuifanMap.get("tcshangxiaozhengquanbu").toString();


			String mmTcJingshang = mmGuifanMap.get("tcjingshang")==null?"":mmGuifanMap.get("tcjingshang").toString();
			String mmTcShangjiangjingshang = mmGuifanMap.get("tcshangjiangjingshang")==null?"":mmGuifanMap.get("tcshangjiangjingshang").toString();
			String mmTcZhongjiangjingshang = mmGuifanMap.get("tczhongjiangjingshang")==null?"":mmGuifanMap.get("tczhongjiangjingshang").toString();
			String mmTcShaojiangzhengjingshang = mmGuifanMap.get("tcshaojiangzhengjingshang")==null?"":mmGuifanMap.get("tcshaojiangzhengjingshang").toString();
			String mmTcShaojiangfujingshang = mmGuifanMap.get("tcshaojiangfujingshang")==null?"":mmGuifanMap.get("tcshaojiangfujingshang").toString();
			String mmTcDaxiaozhengjingshang = mmGuifanMap.get("tcdaxiaozhengjingshang")==null?"":mmGuifanMap.get("tcdaxiaozhengjingshang").toString();
			String mmTcDaxiaofujingshang = mmGuifanMap.get("tcdaxiaofujingshang")==null?"":mmGuifanMap.get("tcdaxiaofujingshang").toString();
			String mmTcShangxiaozhengJingshang = mmGuifanMap.get("tcshangxiaozhengjingshang")==null?"":mmGuifanMap.get("tcshangxiaozhengjingshang").toString();

			String mmTcBenren = mmGuifanMap.get("tcbenren")==null?"":mmGuifanMap.get("tcbenren").toString();
			String mmTcShangjiangBenren = mmGuifanMap.get("tcshangjiangbenren")==null?"":mmGuifanMap.get("tcshangjiangbenren").toString();
			String mmTcZhongjiangBenren = mmGuifanMap.get("tczhongjiangbenren")==null?"":mmGuifanMap.get("tczhongjiangbenren").toString();
			String mmTcShaojiangzhengBenren = mmGuifanMap.get("tcshaojiangzhengbenren")==null?"":mmGuifanMap.get("tcshaojiangzhengbenren").toString();
			String mmTcShaojiangfuBenren = mmGuifanMap.get("tcshaojiangfubenren")==null?"":mmGuifanMap.get("tcshaojiangfubenren").toString();
			String mmTcDaxiaozhengBenren = mmGuifanMap.get("tcdaxiaozhengbenren")==null?"":mmGuifanMap.get("tcdaxiaozhengbenren").toString();
			String mmTcDaxiaofuBenren = mmGuifanMap.get("tcdaxiaofubenren")==null?"":mmGuifanMap.get("tcdaxiaofubenren").toString();
			String mmTcShangxiaozhengBenren = mmGuifanMap.get("tcshangxiaozhengbenren")==null?"":mmGuifanMap.get("tcshangxiaozhengbenren").toString();

			mmResultMap.put("tcquanbu",mmTcQuanbu);
			mmResultMap.put("tcshangjiangquanbu",mmTcShangjiangquanbu);
			mmResultMap.put("tczhongjiangquanbu",mmTcZhongjiangquanbu);
			mmResultMap.put("tcshaojiangzhengquanbu",mmTcShaojiangzhengquanbu);
			mmResultMap.put("tcshaojiangfuquanbu",mmTcShaojiangfuquanbu);
			mmResultMap.put("tcdaxiaozhengquanbu",mmTcDaxiaozhengquanbu);
			mmResultMap.put("tcdaxiaofuquanbu",mmTcDaxiaofuquanbu);
			mmResultMap.put("tcjingshang",mmTcJingshang);
			mmResultMap.put("tcshangjiangjingshang",mmTcShangjiangjingshang);
			mmResultMap.put("tczhongjiangjingshang",mmTcZhongjiangjingshang);
			mmResultMap.put("tcshaojiangzhengjingshang",mmTcShaojiangzhengjingshang);
			mmResultMap.put("tcshaojiangfujingshang",mmTcShaojiangfujingshang);
			mmResultMap.put("tcdaxiaozhengjingshang",mmTcDaxiaozhengjingshang);
			mmResultMap.put("tcdaxiaofujingshang",mmTcDaxiaofujingshang);
			mmResultMap.put("tcshangxiaozhengjingshang",mmTcShangxiaozhengJingshang);
			mmResultMap.put("tcshangxiaozhengquanbu",mmTcShangxiaozhengQuanbu);
			mmResultMap.put("tcbenren",mmTcBenren);
			mmResultMap.put("tcshangjiangbenren",mmTcShangjiangBenren);
			mmResultMap.put("tczhongjiangbenren",mmTcZhongjiangBenren);
			mmResultMap.put("tcshaojiangzhengbenren",mmTcShaojiangzhengBenren);
			mmResultMap.put("tcshaojiangfubenren",mmTcShaojiangfuBenren);
			mmResultMap.put("tcdaxiaozhengbenren",mmTcDaxiaozhengBenren);
			mmResultMap.put("tcdaxiaofubenren",mmTcDaxiaofuBenren);
			mmResultMap.put("tcshangxiaozhengbenren",mmTcShangxiaozhengBenren);

			mmResultMap.put("deptname",mmDeptName);

			if (userfilepath.endsWith("/")) {
				userfilepath = userfilepath.substring(0, userfilepath.length() - 1);
			}
			SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String mmSavePath = userfilepath + "/temp/guifanqingkuang/"+mmDateFormat.format(new Date())+"/";
			File mmFold = new File(mmSavePath);
			if (!mmFold.isDirectory() || !mmFold.exists()) {
				mmFold.mkdirs();
			}

			CreateWord.createWord(userfilepath, mmSavePath,  "guifantongjibiao.docx", mmResultMap, request,
					response,"gftj","1");

		} catch (Exception e) {
			e.printStackTrace();

		}
	}
	// 单位规范汇总表导出
	@RequestMapping("guifanhuizong")
	public void GuifanHuizong(@RequestParam("deptid") String ppDeptId,@RequestParam("renyuanleibie") String ppRenyuanLeibieId,
							  @RequestParam("jigoumingchengma") String ppJigoumingchengma,@RequestParam("paixu") String ppPaixu,
							  HttpServletRequest request,HttpServletResponse response, ServletResponse servletResponse) {
		try {
			ddDaochuService.Guifanhuizong(ppDeptId,ppRenyuanLeibieId,ppJigoumingchengma,ppPaixu,request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
